/*************************************************************
说明: 按键时间处理模块, 采用事件列表组织案件任务.


//Author:陈首敏
*************************************************************/

#define	MID_KEY_C

#include "Global.h"

/*声明结构体======================================*/
/*按键时触发*/
struct	KeyPressClass
{
	unsigned short KeyValue;				//键值
	unsigned short TriggerTime; 			//按键按下相应时间
	unsigned short RepeatTime;				//按下重复响应的间隔时间
	pKeyEventFunc KeyPressFunc;				//对应的按键事件函数
};

/*抬起按键时触发*/
struct	KeyReleaseClass
{
	unsigned short LastKey;			//键值
	unsigned char MinTime; 			//最小抬起时间
	unsigned char MaxTime;			//最大抬起时间
	pKeyEventFunc KeyReleaseFunc;	//对应的抬起按键事件函数
};

struct	KeyPressClass  	 KeyPressTbl[PRESS_KEY_EVENT_NUM];
struct	KeyReleaseClass  KeyReleaseTbl[RELEAE_KEY_EVENT_NUM];

unsigned char KeyPressRegist(unsigned short KeyVal,unsigned short TrigTime, unsigned short RepeatTime, pKeyEventFunc pFunc)
{
	static unsigned char KeyEventNum = 0;

	if(KeyEventNum == 0)
	{
		memset(KeyPressTbl,0, sizeof(KeyPressTbl));
	}
	if((KeyEventNum < PRESS_KEY_EVENT_NUM)
	&&(KeyVal != 0)
	&&(TrigTime > 0)
	&&(pFunc != NULL))
	{
		KeyPressTbl[KeyEventNum].KeyValue = KeyVal;
		KeyPressTbl[KeyEventNum].TriggerTime = TrigTime;
		KeyPressTbl[KeyEventNum].RepeatTime = RepeatTime;
		KeyPressTbl[KeyEventNum].KeyPressFunc = pFunc;
		KeyEventNum ++;
		return 1;
	}
	return 0;
}

unsigned char KeyReleaseRegist(unsigned short KeyVal,unsigned char MinTime, unsigned char MaxTime, pKeyEventFunc pFunc)
{
	static unsigned char KeyEventNum = 0;

	if(KeyEventNum == 0)
	{
		memset(KeyReleaseTbl,0, sizeof(KeyReleaseTbl));
	}
	if((KeyEventNum < PRESS_KEY_EVENT_NUM)
	&&(KeyVal != 0)
	&&(MinTime > 0)
	&&(pFunc != NULL))
	{
		KeyReleaseTbl[KeyEventNum].LastKey = KeyVal;
		KeyReleaseTbl[KeyEventNum].MinTime = MinTime;
		KeyReleaseTbl[KeyEventNum].MaxTime = MaxTime;
		KeyReleaseTbl[KeyEventNum].KeyReleaseFunc = pFunc;
		KeyEventNum ++;
		return 1;
	}
	return 0;
}

/**************************************************************
//Fuction: void KeyScan(unsigned int KeyVal)
//Desrition:按键扫描处理函数
//Input:  
//Output:
//Author:陈首敏
***************************************************************/
void KeyScan(unsigned short KeyVal)
{
	unsigned char i;
	static unsigned short  KeyValBck = 0;
	static unsigned short  KeyDwonCnt = 0;
	static unsigned char KeyRelaseCnt = 0;

	if(KeyVal)
	{
		if(KeyVal != KeyValBck)
		{
			KeyValBck = KeyVal;
			KeyDwonCnt = 0;
		}
		else
		{
			KeyRelaseCnt = 0;
			if(KeyDwonCnt != 0xffff)KeyDwonCnt ++;
			for( i = 0 ; i < PRESS_KEY_EVENT_NUM ; i++ )
			{
				if(KeyPressTbl[i].KeyValue == 0)break;
				if(KeyPressTbl[i].KeyValue == KeyVal)
				{
					if(KeyPressTbl[i].TriggerTime == KeyDwonCnt)
					{
						(KeyPressTbl[i].KeyPressFunc)();
						break;
					}
					else if(KeyPressTbl[i].RepeatTime)
					{
						if(KeyDwonCnt > (KeyPressTbl[i].TriggerTime + KeyPressTbl[i].RepeatTime))
						{
							(KeyPressTbl[i].KeyPressFunc)();
							KeyDwonCnt -= KeyPressTbl[i].RepeatTime;
							break;
						}
					}
				}
			}
		}
	}
	else
	{
		if(KeyRelaseCnt != 0xff)KeyRelaseCnt ++;
		if(KeyRelaseCnt == 3)
		{
			for( i = 0 ; i < RELEAE_KEY_EVENT_NUM ; i++ )
			{
				if(KeyReleaseTbl[i].LastKey == 0)break;
				if(KeyReleaseTbl[i].LastKey == KeyValBck)
				{
					if((KeyReleaseTbl[i].MinTime <= KeyDwonCnt)
					&&(KeyDwonCnt <= KeyReleaseTbl[i].MaxTime))
					{
						(KeyReleaseTbl[i].KeyReleaseFunc)();
						break;
					}
				}
			}
			KeyDwonCnt = 0;
			KeyValBck = 0;
		}
	}
}

/**************************************************************
//Fuction: void KeyProc(void)
//Desrition:按键模块总入口
//Input:  
//Output:
//Author:陈首敏
***************************************************************/
void KeyProc(unsigned short KeyVal)
{
	if(GetTimeTick(TIME_10ms))
	{
		KeyScan(KeyVal);
	}
}
